﻿using System;

namespace YawetagLib.Data.Xml
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
    public class XReferenceAttribute : Attribute
    {
        public XReferenceAttribute(Type referencedEntityType, XReferenceType referenceType)
        {
            ReferencedEntityType = referencedEntityType;
            ReferenceType = referenceType;
            ForeignKeyName = GetDefaultForeignKeyName();

            IsLasyLoading = true;
        }

        public XReferenceAttribute(Type referencedEntityType)
            : this(referencedEntityType, XReferenceType.OneToMany)
        {
        }

        private string GetDefaultForeignKeyName() 
        {
            return ReferenceType == XReferenceType.OneToOne
                ? ReferencedEntityType.GetXEntityAttribute().PrimaryKeyName
                : string.Empty;
        }

        public Type ReferencedEntityType { get; private set; }
        public XReferenceType ReferenceType { get; private set; }
        public string ForeignKeyName { get; set; }

        public bool IsLasyLoading { get; set; }
    }

    public enum XReferenceType { OneToOne, OneToMany }
}
